import logging
import time
import sthz as gb
from dm import dm
import aStart as ast
import random
import util as ul

is_press_up=False
is_press_down=False
is_press_left=False
is_press_right=False
room_id = 8
global_real_x=0
global_real_y=0
global_get_pos=False
dxc_gw=None
dxc_door=None
mini_map = ['./hbl/hbl_1.bmp','./hbl/hbl_2.bmp','./hbl/hbl_3.bmp','./hbl/hbl_4.bmp','./hbl/hbl_5.bmp','./hbl/hbl_6.bmp','./hbl/blp_7.bmp','./hbl/hbl_8.bmp','./hbl/hbl_9.bmp','./hbl/hbl_10.bmp']
def loopSearch(fn=None,*args,**argss):
     while True:
        mpa1_wz = fn(*args,**argss)
        if len(mpa1_wz)>0:
            return mpa1_wz
        else:
            print('找不到获取真实坐标的文字')

def user_in_hbl_which_map():
    id = -1
    boss = None
    user = None
    ret = dm.FindStr(0,0,1024,768, '堇' ,"f8f800-123456|fb9800-123456",1)
    if ret[0]!=-1:
        print("检测到堇")
        return 11
    while True:
        boss = dm.FindPic(0,0,1024,768,'./nyz_dt/boss.bmp',"123456",1,2)
        if boss[0]!=-1:
            break
        else:
            print('房间检测 boss',room_id)
            xr = dm.FindPic(0,0,1024,768,"./nyz_dt/rwxr.bmp","123456",0.9,0)
            if xr[0]!=-1:
              return -1 
            if room_id == 13 or room_id == 14:
                return 14
            zctz = dm.FindPic(0,0,1024,768,"./nyz_dt/zctz.bmp","123456",1,2)
            if zctz[0]!=-1  or room_id==11:
                return 11
            
    while True:        
        user = dm.FindPic(0,0,1024,768,"./nyz_dt/人.bmp|./nyz_dt/liefeng.bmp","123456",1,2)
        if user[0]!=-1:
            if user[0]==0:
                cur_y = int(user[2])
                cur_x = int(user[1])
                if cur_y-boss[2]>46:
                    id=1
                elif cur_y-boss[2]>28 and cur_y-boss[2]<48:
                    # 图  2 3 4
                    if boss[1]-cur_x<59 and boss[1]-cur_x>39:
                        id = 2
                    elif  boss[1]-cur_x<41 and boss[1]-cur_x>21:
                        id = 3
                    else:
                        id = 4
                elif cur_y-boss[2]>10 and cur_y-boss[2]<30:
                    if boss[1]-cur_x<59 and boss[1]-cur_x>39:
                        id = 7
                    elif  boss[1]-cur_x<41 and boss[1]-cur_x>21:
                        id = 6
                    else:
                        id = 5
                else:
                    if boss[1]-cur_x<59 and boss[1]-cur_x>39:
                        id = 8
                    elif  boss[1]-cur_x<41 and boss[1]-cur_x>21:
                        id = 9
                    else:
                        id = 10
            else:
                # 裂缝
                id = 13
            break
        else:
            zctz = dm.FindPic(0,0,1024,768,"./nyz_dt/zctz.bmp","123456",1,2)
            print('房间检测 蓝色小人')
            xr = dm.FindPic(0,0,1024,768,"./nyz_dt/rwxr.bmp","123456",0.9,0)
            if xr[0]!=-1:
              return -1 
            if zctz[0]!=-1 or not ul.check_has_pl(dm):
                return 11
            boss_end = dm.FindPic(0,0,1024,768,'./nyz_dt/boss_end_1.bmp|./nyz_dt/boss_end_2.bmp',"123456",1,2)
            if  boss_end[0]!=-1:
                return 11
            if room_id == 13:
                return 14
    return id

def user_pos():
    pos_strs = dm.FindStrFastEx(0,0,1024,768, "移|捕|获|叛|蠲|璇|玑|悬|斡|陸|哋|彯|夷|埔|诛|斩|盗|贼|操|刃|持|斧|虎|符|越|剑|誅|斬|稻|賊|襙|轫|馳|滏|唬|俯|樾|劍","f8f800-000000|ffff79-000000|ffff00-000000",1)
    x = 0
    y = 0
    print(pos_strs)
    for pos_str in  pos_strs.split("|"):
        pos_arr = pos_str.split(",")
        if pos_arr[0]=='1':
            x = int(pos_arr[1])+48
            y = int(pos_arr[2])+50+110
        elif pos_arr[0]=='0':
            x = int(pos_arr[1])-24
            y = int(pos_arr[2])+100+110
        elif pos_arr[0]=='2':
            x = int(pos_arr[1])+24
            y = int(pos_arr[2])+50+110
        elif pos_arr[0]=='3':
            x = int(pos_arr[1])
            y = int(pos_arr[2])+50+110
        elif pos_arr[0]=='4':
            x = int(pos_arr[1])-24
            y = int(pos_arr[2])+50+110
        elif pos_arr[0]=='5':
            x = int(pos_arr[1])+48
            y = int(pos_arr[2])+110
        elif pos_arr[0]=='6':
            x = int(pos_arr[1])+24
            y = int(pos_arr[2])+110
        elif pos_arr[0]=='8':
            x = int(pos_arr[1])-24
            y = int(pos_arr[2])+110
        elif pos_arr[0]=='9':
            # 434 444
            x = int(pos_arr[1])-35
            y = int(pos_arr[2])+208
        elif pos_arr[0]=='10':
            x = int(pos_arr[1])-11
            y = int(pos_arr[2])+208
        elif pos_arr[0]=='11':
            x = int(pos_arr[1])+23
            y = int(pos_arr[2])+208
        elif pos_arr[0]=='12':
            x = int(pos_arr[1])+38
            y = int(pos_arr[2])+208
        elif pos_arr[0]=='13':
            x = int(pos_arr[1])-35
            y = int(pos_arr[2])+160
        elif pos_arr[0] == '14':
            x = int(pos_arr[1]) + 42
            y = int(pos_arr[2]) + 200
        elif pos_arr[0] == '15':
            x = int(pos_arr[1]) + 17
            y = int(pos_arr[2]) + 200
        elif pos_arr[0] == '16':
            x = int(pos_arr[1]) - 5
            y = int(pos_arr[2]) + 200
        elif pos_arr[0] == '17':
            x = int(pos_arr[1]) - 31
            y = int(pos_arr[2]) + 200
        elif pos_arr[0] == '18':
            x = int(pos_arr[1]) + 42
            y = int(pos_arr[2]) + 152
        elif pos_arr[0] == '19':
            x = int(pos_arr[1]) + 17
            y = int(pos_arr[2]) + 151
        elif pos_arr[0] == '20':
            x = int(pos_arr[1]) - 5
            y = int(pos_arr[2]) + 152
        elif pos_arr[0] == '21':
            x = int(pos_arr[1]) - 30
            y = int(pos_arr[2]) + 152
        elif pos_arr[0] == '22':
            x = int(pos_arr[1]) + 0
            y = int(pos_arr[2]) + 103
        elif pos_arr[0] == '23':
            x = int(pos_arr[1]) + 17
            y = int(pos_arr[2]) + 103
        elif pos_arr[0] == '24':
            x = int(pos_arr[1]) + 6
            y = int(pos_arr[2]) + 103
        elif pos_arr[0] == '25':
            x = int(pos_arr[1]) - 31
            y = int(pos_arr[2]) + 103
        elif pos_arr[0] == '29':
            x = int(pos_arr[1]) + 44
            y = int(pos_arr[2]) + 200
        elif pos_arr[0] == '28':
            x = int(pos_arr[1]) + 19
            y = int(pos_arr[2]) + 200
        elif pos_arr[0] == '27':
            x = int(pos_arr[1]) - 3
            y = int(pos_arr[2]) + 200
        elif pos_arr[0] == '26':
            x = int(pos_arr[1]) - 28
            y = int(pos_arr[2]) + 200
        elif pos_arr[0] == '33':
            x = int(pos_arr[1]) + 44
            y = int(pos_arr[2]) + 152
        elif pos_arr[0] == '32':
            x = int(pos_arr[1]) + 19
            y = int(pos_arr[2]) + 152
        elif pos_arr[0] == '31':
            x = int(pos_arr[1]) - 3
            y = int(pos_arr[2]) + 151
        elif pos_arr[0] == '30':
            x = int(pos_arr[1]) - 28
            y = int(pos_arr[2]) + 152
        elif pos_arr[0] == '37':
            x = int(pos_arr[1]) + 44
            y = int(pos_arr[2]) + 103
        elif pos_arr[0] == '36':
            x = int(pos_arr[1]) + 19
            y = int(pos_arr[2]) + 103
        elif pos_arr[0] == '35':
            x = int(pos_arr[1]) - 3
            y = int(pos_arr[2]) + 103
        elif pos_arr[0] == '34':
            x = int(pos_arr[1]) - 28
            y = int(pos_arr[2]) + 103
    return (x,y)

def user_hbl_real_pos(x,y):
    global room_id
    def search_real_pos_txt(str,dir=1):
        ret = dm.FindStrFast(0,0,1024,1024, str ,"f8f800-123456|00a0e8-123456",dir)
        return ret
    real_x = 0
    real_y = 0
    if room_id==1 or room_id==11:
        base_list = [(443,226),(643,227),(1244,226)]
        base_x = 643 #636
        base_y = 227 #227
        mpa1_wz = loopSearch(search_real_pos_txt, "暗黑1鳮|暗黑1継|痛苦蘇")
        if mpa1_wz[0]!=-1:
            base_x = base_list[int(mpa1_wz[0])][0]
            base_y = base_list[int(mpa1_wz[0])][1]
        move_x = base_x-int(mpa1_wz[1])
        move_y = base_y-int(mpa1_wz[2])
        real_x = x+move_x
        real_y = y+move_y
    elif room_id == 2 or room_id == 3 or room_id == 6:
        base_list = [(527,216),(531,216),(727,216),(727,216)]
        base_x = 727
        base_y = 216
        mpa1_wz = loopSearch(search_real_pos_txt, "暗黑1龍|暗黑1龙|暗黑1鑫|鑫上鑫上")
        print(mpa1_wz)
        if mpa1_wz[0]!=-1:
            base_x = base_list[int(mpa1_wz[0])][0]
            base_y = base_list[int(mpa1_wz[0])][1]
        move_x = base_x-int(mpa1_wz[1])
        move_y = base_y-int(mpa1_wz[2])
        real_x = x+move_x
        real_y = y+move_y
    elif room_id == 4 or room_id == 5 or room_id == 9 or room_id == 10 or room_id==13:
        base_list = [(367,218),(666,216),(965,216)]
        base_x = 666
        base_y = 216
        mpa1_wz = loopSearch(search_real_pos_txt, "馿馿馿馿|暗黑1鳮|暗黑1榖")
        if mpa1_wz[0]!=-1:
            base_x = base_list[int(mpa1_wz[0])][0]
            base_y = base_list[int(mpa1_wz[0])][1]
        move_x = base_x-int(mpa1_wz[1])
        move_y = base_y-int(mpa1_wz[2])
        real_x = x+move_x
        real_y = y+move_y
    elif room_id == 7:
        base_list = [(361,228),(660,228)]
        base_x = 361
        base_y = 228
        mpa1_wz = loopSearch(search_real_pos_txt, "馿馿馿馿|荆棘蘇")
        if mpa1_wz[0]!=-1:
            base_x = base_list[int(mpa1_wz[0])][0]
            base_y = base_list[int(mpa1_wz[0])][1]
        move_x = base_x-int(mpa1_wz[1])
        move_y = base_y-int(mpa1_wz[2])
        real_x = x+move_x
        real_y = y+move_y
    elif room_id == 8:
        base_list = [(327,208),(680,208)]
        base_x = 327
        base_y = 208
        mpa1_wz = loopSearch(search_real_pos_txt, "矿山輪|荆棘蘇")
        if mpa1_wz[0]!=-1:
            base_x = base_list[int(mpa1_wz[0])][0]
            base_y = base_list[int(mpa1_wz[0])][1]
        move_x = base_x-int(mpa1_wz[1])
        move_y = base_y-int(mpa1_wz[2])
        real_x = x+move_x
        real_y = y+move_y
    else:
        real_x = 0
        real_y = 0
    return (real_x//10,real_y//10)

def get_real_pos():
    global global_real_x
    global global_real_y
    global global_get_pos
    p_x,p_y = user_pos()
    print('相对位置',p_x,p_y)
    x,y = user_hbl_real_pos(p_x,p_y)
    print('绝对位置',x,y)
    if p_x==0 and p_y==0 and x==0 and y==0:
         gb.presskeybyvalue(37)
         time.sleep(0.2)
         gb.releasekeybyvalue(37)
    global_real_x = x
    global_real_y = y

def move_user(x,y,tx,ty,isRun):
    global is_press_up
    global is_press_down
    global is_press_left
    global is_press_right
    #print(gb.presskeybyvalue(91)) 37 38 39 40 左开始
    #print(gb.releasekeybyvalue(91))
    if x>tx:
        if not is_press_left:
            if isRun:
                gb.pressandreleasekeybyvalue(37)
                time.sleep(0.3)
            gb.presskeybyvalue(37)
            gb.releasekeybyvalue(39)
            is_press_left=True
            is_press_right=False
    elif x<tx:
        if not is_press_right:
            if isRun:
                gb.pressandreleasekeybyvalue(39)
                time.sleep(0.3)
            gb.presskeybyvalue(39)
            gb.releasekeybyvalue(37)
            is_press_left=False
            is_press_right=True
    else:
         gb.releasekeybyvalue(39)
         gb.releasekeybyvalue(37)
         is_press_left=False
         is_press_right=False
    if y>ty:
        if not is_press_up:
            gb.presskeybyvalue(38)
            gb.releasekeybyvalue(40)
            is_press_up=True
            is_press_down=False
    elif y<ty:
        if not is_press_down:
            gb.presskeybyvalue(40)
            gb.releasekeybyvalue(38)
            is_press_down=True
            is_press_up=False
    else:
        gb.releasekeybyvalue(40)
        gb.releasekeybyvalue(38)
        is_press_up=False
        is_press_down=False

def resetKey():
    global is_press_up
    global is_press_down
    global is_press_left
    global is_press_right
    gb.releasekeybyvalue(37)
    gb.releasekeybyvalue(38)
    gb.releasekeybyvalue(39)
    gb.releasekeybyvalue(40)
    is_press_up=False
    is_press_down=False
    is_press_left=False
    is_press_right=False

def check_is_toogle_room():
    global room_id
    cur_id = user_in_hbl_which_map()
    if cur_id != room_id:
        room_id = cur_id
        return True
    return False

def move_to_target(x,y,isRun=False):
    global room_id
    global global_get_pos
    global global_real_x
    global global_real_y
    global mini_map
    global_get_pos=True
    if room_id == 10:
        map = mini_map[room_id-2]
    elif room_id == 13:
        map = mini_map[room_id-5]
    elif room_id == 11:
        map = mini_map[room_id-3]
    else:
        map = mini_map[room_id-1]
    get_real_pos()
    im,w,h=ast.pretreatment(map)  #调用图像预处理函数
    mapData = ast.Array2D(w, h, im)
    if isRun and global_real_x == x and global_real_y == y:
         gb.presskeybyvalue(37)
         time.sleep(0.5)
         gb.releasekeybyvalue(37)

    print(f'当前位置{global_real_x} {global_real_y} 目标位置{x} {y}')
    arr = ast.AStart(mapData, ast.Point( global_real_y, global_real_x), ast.Point(y, x), 0)
    if(arr==-1):
        gb.presskeybyvalue(37)
        time.sleep(0.2)
        gb.releasekeybyvalue(37)
        gb.presskeybyvalue(38)
        time.sleep(0.2)
        gb.releasekeybyvalue(38)
        gb.presskeybyvalue(39)
        time.sleep(0.2)
        gb.releasekeybyvalue(39)
        gb.presskeybyvalue(40)
        time.sleep(0.2)
        gb.releasekeybyvalue(40)
        print("终点是障碍物")
        return
    if arr == None:
        gb.presskeybyvalue(37)
        time.sleep(0.2)
        gb.releasekeybyvalue(37)
        gb.presskeybyvalue(38)
        time.sleep(0.2)
        gb.releasekeybyvalue(38)
        gb.presskeybyvalue(39)
        time.sleep(0.2)
        gb.releasekeybyvalue(39)
        gb.presskeybyvalue(40)
        time.sleep(0.2)
        gb.releasekeybyvalue(40)
        print(f"获取最短路径失败{global_real_x} {global_real_y} {x} {y}")
        return
    # 通过a星寻路获取到所有的路线
    # 根据路线流 走到相应的位置
    step = 4 
    arg = (len(arr)-1)%step
    index=0
    while(True):
        get_real_pos()
        if index>=len(arr)-3:
            resetKey()
            break
        pos = arr[index]

        if (len(arr)-1-index)==arg:
            if arg!=0:
                index+=arg
            else:
                index+=2
        else:
            index+=step 

        if  global_real_x>pos[0] and is_press_right:
            continue
        if  global_real_x<pos[0] and is_press_left:
            continue
        if global_real_x==0 and global_real_y==0:
            resetKey()
            continue
        move_user(global_real_x,global_real_y,pos[0],pos[1],isRun)
        if check_is_toogle_room():
            resetKey()
            break
        
        if isRun:
            time.sleep(0)
        else:          
            time.sleep(0.02)            

def search_gw():
    global dxc_gw
    gw = dm.FindPic(0,0,1024,768,'./nyz_dt/gw.bmp',"123456",1,2)
    if gw[0]!=-1:
            dxc_gw = (int(gw[1]),int(gw[2]))
   
def search_door():
  global dxc_door
  door = dm.FindPic(0,0,1024,768,'./nyz_dt/gw.bmp',"123456",1,2)
  if door[0]!=-1:
            dxc_door = (int(door[1]),int(door[2]))




